
package server.life;

import java.io.File;
import java.util.HashMap;
import java.util.Map;
import provider.PureData;
import provider.PureDataProvider;
import provider.PureDataProviderFactory;
import provider.PureDataTool;
import tools.Pair;
import tools.StringUtil;

public class MobAttackInfoFactory {

    private static final MobAttackInfoFactory instance = new MobAttackInfoFactory();
    private static final PureDataProvider dataSource = PureDataProviderFactory.getDataProvider(new File("wz/Mob.wz"));
    private static Map<Pair<Integer, Integer>, MobAttackInfo> mobAttacks = new HashMap<Pair<Integer, Integer>, MobAttackInfo>();

    public static MobAttackInfoFactory getInstance() {
        return instance;
    }

    public MobAttackInfo getMobAttackInfo(PureMonster mob, int attack) {
        MobAttackInfo ret = mobAttacks.get(new Pair<Integer, Integer>(Integer.valueOf(mob.getId()), Integer.valueOf(attack)));
        if (ret != null) {
            return ret;
        }

        PureData mobData = dataSource.getData(StringUtil.getLeftPaddedStr(Integer.toString(mob.getId()) + ".img", '0', 11));
        if (mobData != null) {
            PureData infoData = mobData.getChildByPath("info/link");
            if (infoData != null) {
                String linkedmob = PureDataTool.getString("info/link", mobData);
                mobData = dataSource.getData(StringUtil.getLeftPaddedStr(linkedmob + ".img", '0', 11));
            }
            final PureData attackData = mobData.getChildByPath("attack" + (attack + 1) + "/info");
            if (attackData != null) {
                ret = new MobAttackInfo(mob.getId(), attack);
                ret.setDeadlyAttack(attackData.getChildByPath("deadlyAttack") != null);
                ret.setMpBurn(PureDataTool.getInt("mpBurn", attackData, 0));
                ret.setDiseaseSkill(PureDataTool.getInt("disease", attackData, 0));
                ret.setDiseaseLevel(PureDataTool.getInt("level", attackData, 0));
                ret.setMpCon(PureDataTool.getInt("conMP", attackData, 0));
            }
        }
        mobAttacks.put(new Pair<Integer, Integer>(Integer.valueOf(mob.getId()), Integer.valueOf(attack)), ret);

        return ret;
    }
}
